/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import Model.Uzivatel;
import ModelJava.UserManager;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author papa
 */
public class ControllerLogin extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String viewUrl = "/index.jsp";
        //zaridi ze po prihlaseni zustanu na stejne jsp strance
        request.setAttribute("showPage", request.getParameter("oldPage"));
        try {
            HttpSession session = request.getSession();
            System.err.println("login");
            //Pokud se chce uzivatel prihlasit
            if(request.getParameter("odeslat").equals("Prihlasit")){
                //validace loginu
                if(validateName(request.getParameter("jmeno"), request.getParameter("heslo"), session)){
                    //je validni potrebuju zmenit kolonku prihlaseni na prihlasen
                    if(((Uzivatel)session.getAttribute("uzivatel")).getRole().getNazev().equals("zákazník")){
                        session.setAttribute("login", "usermode");
                        System.err.println("usermode");
                    }else if(((Uzivatel)session.getAttribute("uzivatel")).getRole().getNazev().equals("administrátor")){
                        session.setAttribute("login", "adminmode");
                        System.err.println("adminmode");
                    }
                }
            }
            //odhlasovani uzivatele
            if(request.getParameter("odeslat").equals("Odhlasit")){
                session.removeAttribute("login");
                session.removeAttribute("uzivatel");
            }
            RequestDispatcher rd = getServletContext().getRequestDispatcher(viewUrl);
            rd.forward(request, response);
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    /**
     * kontrola zda jmeno je v databazi
     * @param jmeno jmeno ke kontrole
     * @return kdyz je v DB vraci true
     */
    private boolean validateName(String jmeno, String heslo, HttpSession session) {
        boolean isValid = false;
        Uzivatel u = null;
        u = new UserManager().isNameValid(jmeno);
        if(u != null){
            //validace hesla
            System.err.println("jmeno platne");
            isValid = validatePassword(jmeno, heslo);
            System.err.println(isValid);
            session.setAttribute("uzivatel", u);
            vypisInfo(u);
        }
        return isValid;
    }

    void vypisInfo(Uzivatel u){
        System.err.println("jmeno: "+u.getJmeno());
        System.err.println(": "+u.getPrijmeni());
        System.err.println(": "+u.getEmail());
        System.err.println(": "+u.getHeslo());
        System.err.println(": "+u.getAdresa().getMesto());
        System.err.println(": "+u.getAdresa().getStat());
        System.err.println(": "+u.getAdresa().getUlice());
        System.err.println(": "+u.getAdresa().getCp());
        System.err.println(": "+u.getAdresa().getIda());
        System.err.println(": "+u.getAdresa().getPsc());
        System.err.println(": "+u.getIdu());
        System.err.println(": "+u.getRole().getNazev());
        System.err.println(": "+u.getRole().getIdr());
        System.err.println(": "+u.getTelefon());
    }

    /**
     * kontrola zda souhlasi heslo u jmena
     * @param heslo heslo ke kontrole
     * @return true pokud heslo souhlasi
     */
    private boolean validatePassword(String jmeno, String heslo) {
        boolean isValid = false;
        isValid = new UserManager().isPasswordValid(jmeno, heslo);
        return isValid;
    }

}
